const router = require("koa-router")();
const {
  register,
  checkName,
  getIpInfo,
  login,
  getUsers,
  getClassList,
  getUser,
  getVideoList,
  updateUser,
  deleteUsers,
  getVideoDetail,
  getVideoQueryList,
  updateWatch,
  getAllUsers,
  getCollection,
  doCollection,
  getHistoryList,
  getHotVideo,
  getCollectionList,
} = require("../controller/web");

router.prefix("/web");

function handleRes(ctx, next, res) {
  if (res.status === 0) {
    ctx.body = res;
  } else {
    ctx.status = res.httpCode;
    ctx.body = res;
    // ctx.message = res.message   //本来想直接设置fetch的statusText，但是加了这句话请求就出错
  }
}

router.post("/register", async function (ctx, next) {
  const { phone, password } = ctx.request.body;
  console.log(ctx.request.body);
  const res = await register(phone, password, ctx);
  handleRes(ctx, next, res);
});

router.post("/login", async function (ctx, next) {
  const { phone, password } = ctx.request.body;
  const res = await login(phone, password, ctx);
  handleRes(ctx, next, res);
});

router.get("/checkName", async function (ctx, next) {
  const { username } = ctx.query;
  const res = await checkName(username);
  handleRes(ctx, next, res);
});

router.get("/watchUpdate", async function (ctx, next) {
  const { id, phone = "" } = ctx.query;
  const res = await updateWatch(id, phone);
  handleRes(ctx, next, res);
});

router.get("/doCollection", async function (ctx, next) {
  const { id, phone = "" } = ctx.query;
  const res = await doCollection(id, phone);
  handleRes(ctx, next, res);
});

router.get("/collectionStatus", async function (ctx, next) {
  const { id, phone = "" } = ctx.query;
  const res = await getCollection(id, phone);
  handleRes(ctx, next, res);
});

router.get("/getUser", async function (ctx, next) {
  const res = await getUser(ctx.query);
  handleRes(ctx, next, res);
});

router.get("/class", async function (ctx, next) {
  const res = await getClassList();
  handleRes(ctx, next, res);
});

router.get("/search", async function (ctx, next) {
  const { title } = ctx.query;
  const res = await getVideoQueryList(title);
  handleRes(ctx, next, res);
});

router.get("/videoList", async function (ctx, next) {
  const { id } = ctx.query;
  console.log(id, "iddddd");
  const res = await getVideoList(+id);
  handleRes(ctx, next, res);
});

router.get("/history", async function (ctx, next) {
  const { phone } = ctx.query;
  const res = await getHistoryList(phone);
  handleRes(ctx, next, res);
});

router.get("/hotVideo", async function (ctx, next) {
  const res = await getHotVideo();
  handleRes(ctx, next, res);
});
router.get("/collectionList", async function (ctx, next) {
  const { phone } = ctx.query;
  const res = await getCollectionList(phone);
  handleRes(ctx, next, res);
});

router.get("/videoInfo", async function (ctx, next) {
  const { id } = ctx.query;
  console.log(id, "iddddd");
  const res = await getVideoDetail(+id);
  handleRes(ctx, next, res);
});

router.get("/getIpInfo", async function (ctx, next) {
  const res = await getIpInfo(ctx);
  handleRes(ctx, next, res);
});

router.get("/getUsers", async function (ctx, next) {
  const res = await getUsers(ctx.query);
  handleRes(ctx, next, res);
});

router.get("/getUser", async function (ctx, next) {
  const res = await getUser(ctx.query);
  handleRes(ctx, next, res);
});

router.post("/update", async function (ctx, next) {
  const sessionId = ctx.cookies.get("sessionId");
  const res = await updateUser(ctx.request.body, sessionId);
  handleRes(ctx, next, res);
});

router.post("/delete", async function (ctx, next) {
  const res = await deleteUsers(ctx.request.body);
  handleRes(ctx, next, res);
});

router.get("/getAllUsers", async function (ctx, next) {
  const res = await getAllUsers();
  handleRes(ctx, next, res);
});

module.exports = router;
